package liuliu.throughwaring.ui.activity;

import android.view.View;
import android.widget.Button;

import net.tsz.afinal.annotation.view.CodeNote;

import liuliu.custom.control.edittext.ImageEditText;
import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.model.Key;

/**
 * Created by liuliu on 2015/11/13   14:41
 * <p/>
 * 网络设置
 *
 * @author 柳伟杰
 * @Email 1031066280@qq.com
 */
public class SettingActivity extends BaseActivity {
    public static SettingActivity mInstance;
    @CodeNote(id = R.id.setting_ip)
    ImageEditText setting_ip;
    @CodeNote(id = R.id.setting_port)
    ImageEditText setting_port;
    @CodeNote(id = R.id.setting_http_submit, click = "onClick")
    Button submit;
    @CodeNote(id = R.id.setting_toolbar)
    TToolbar mToolbar;

    String isSetting = "";

    @Override
    public void initViews() {
        setContentView(R.layout.activity_setting);
        mInstance = this;
        isSetting = getIntent().getStringExtra("Setting");
    }

    @Override
    public void initEvents() {
        if (isSetting != null) {
            setting_ip.setText(Utils.ReadString(Key.KEY_IP));
            setting_port.setText(Utils.ReadString(Key.KEY_PORT));
        } else {
            if (!Utils.ReadString(Key.KEY_IP).equals("") && !Utils.ReadString(Key.KEY_PORT).equals("")) {
                mUtils.IntentPost(LoadingActivity.class);
                mInstance.finish();
            } else {
                setting_ip.setText(Utils.ReadString(Key.KEY_IP));
                setting_port.setText(Utils.ReadString(Key.KEY_PORT));
            }
        }
        mToolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                mInstance.finish();
            }
        });
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.setting_http_submit:
                String ip = setting_ip.getText().trim();
                String port = setting_port.getText().trim();
                if (ip.equals("") || port.equals("")) {
                    mUtils.ToastShort("ip或者端口不能为空！");
                } else {
                    mUtils.WriteString(Key.KEY_IP, ip);
                    mUtils.WriteString(Key.KEY_PORT, port);
                    mUtils.IntentPost(LoadingActivity.class);
                    mInstance.finish();
                }
                break;
        }
    }

}
